

-- To do:
--  Runes
--  Arcane Charges
--  Burning Embers
--  Chi
--  Demonic Fury
--  Holy Power
--  Lunar/Solar Energy
--  Soul Shards
--  Shadow Orbs
--  Verdant Spheres


-- Namespace
	local _,mod = ...


-- Object reference
	local units = mod.units
	local resolve = mod.resolve


-- Common
	local _,class = UnitClass('player')


-- Health
	local care = mod:unitevent_care('health')
	if care then
		local function HEALTH(unit)
			if not care[unit] then return end
			local u = units[unit]
			
			local min = UnitHealth(unit)
			local max = UnitHealthMax(unit)
			
			if max == 0 then
				min,max = .001,1
			end
			
			u:Update('health',min,max)
		end
		
		mod:event_register('UNIT_HEALTH',HEALTH)
		mod:event_register('UNIT_MAXHEALTH',HEALTH)
		
		resolve[#resolve+1] = HEALTH
	end


-- Power
	local care = mod:unitevent_care('power')
	if care then
		local function POWER(unit)
			if not care[unit] then return end
			local u = units[unit]
			
			local min = UnitPower(unit)
			local max = UnitPowerMax(unit)
			
			if max == 0 then
				min,max = .001,1
			end
			
			u:Update('power',min,max)
		end
		
		mod:event_register('UNIT_POWER',POWER)
		mod:event_register('UNIT_MAXPOWER',POWER)
		mod:event_register('UNIT_DISPLAYPOWER',POWER)
		
		mod:timer_register(0,true,function()
			POWER('player')
		end)
		
		resolve[#resolve+1] = POWER
	end


-- Druid mana
	if class == 'DRUID' then
		local care = mod:unitevent_care('mana')
		if care and care['player'] then
			local function MANA(unit,ptype)
				if unit ~= 'player' then return end
				
				local u = units[unit]
				
				if ptype and ptype ~= 'MANA' then return end
				
				if UnitPowerType(unit) == SPELL_POWER_MANA then
					u:Update('mana',nil)
				else
					local min = UnitPower(unit,SPELL_POWER_MANA)
					local max = UnitPowerMax(unit,SPELL_POWER_MANA)
					
					if max == 0 then
						min,max = .001,1
					end
					
					u:Update('mana',min,max)
				end
			end
			
			mod:event_register('UNIT_POWER',MANA)
			mod:event_register('UNIT_MAXPOWER',MANA)
			mod:event_register('UNIT_DISPLAYPOWER',MANA)
		end
	end


-- Incoming heal
	local care = mod:unitevent_care('incheal')
	if care then
		local function INCHEAL(unit)
			if not care[unit] then return end
			local u = units[unit]
			
			local h = UnitGetIncomingHeals(unit)
			local min,max
			if h and h > 0 then
				min = UnitHealth(unit)
				max = UnitHealthMax(unit)
				if max == 0 then
					min,max = .001
				end
			else
				h = nil
			end
			
			u:Update('incheal',h,min,max)
		end
		
		mod:event_register('UNIT_HEAL_PREDICTION',INCHEAL)
		mod:event_register('UNIT_HEALTH',INCHEAL)
		mod:event_register('UNIT_MAXHEALTH',INCHEAL)
		
		resolve[#resolve+1] = INCHEAL
	end


-- Combo points
	local care = mod:unitevent_care('combopoints')
	if care then
		local function COMBOPOINTS(unit)
			local cp = GetComboPoints('player',unit)
			if not cp then
				cp = 0
			end
			units[unit]:Update('combopoints',cp)
		end
		
		mod:event_register('UNIT_COMBO_POINTS',function(unit)
			if unit and unit ~= 'player' then return end
			for unit in pairs(care) do
				COMBOPOINTS(unit)
			end
		end)
		
		resolve[#resolve+1] = function(unit)
			if care[unit] then
				COMBOPOINTS(unit)
			end
		end
	end


-- Anticipation
	local care = mod:unitevent_care('anticipation')
	if care then
		local function ANTICIPATION()
			local acp = (select(4,UnitAura('player','Anticipation')))
			if not acp then
				acp = 0
			end
			
			for unit in pairs(care) do
				units[unit]:Update('anticipation',acp)
			end
		end
		
		local ev = {
			['SPELL_AURA_APPLIED'] = true,
			['SPELL_AURA_REMOVED'] = true,
			['SPELL_AURA_APPLIED_DOSE'] = true,
			['SPELL_AURA_REMOVED_DOSE'] = true,
			['SPELL_AURA_REFRESH'] = true,
		}
		local player
		
		mod:event_register('COMBAT_LOG_EVENT_UNFILTERED',function(_,event,_,src,_,_,_,dest,_,_,_,_,spell)
			if ev[event] and src == player and dest == player and spell == 'Anticipation' then
				ANTICIPATION()
			end
		end)
		
		mod:event_register('UNIT_COMBO_POINTS',ANTICIPATION)
		
		mod:event_register('PLAYER_ENTERING_WORLD',function()
			player = UnitGUID('player')
			ANTICIPATION()
		end)
	end
